package com.graduation.fq.teacher.logic;

import com.graduation.fq.teacher.interfaces.EventListener;
import com.graduation.fq.teacher.interfaces.ResponseListener;
import com.graduation.fq.teacher.module.LoginEventArgs;
import com.graduation.fq.teacher.module.SimpleEventArgs;
import com.graduation.fq.teacher.process.LoginRegisterProcess;
import com.graduation.fq.teacher.utils.LoginRegisterIndex;

/**
 * @author jifang
 * @version 2016/5/16.
 */
public class LoginRegisterLogic extends BaseLogic {
    private static LoginRegisterLogic instance;

    public static LoginRegisterLogic getInstance() {
        if (instance == null) {
            instance = new LoginRegisterLogic();
        }
        return instance;
    }

    /**
     * 登录
     *
     * @param name
     * @param pwd
     * @param listener
     */
    public void login(String name, final String pwd, final EventListener listener) {
        final LoginRegisterProcess process = new LoginRegisterProcess(LoginRegisterIndex.LOGIN, name, pwd);
        process.runGet(new ResponseListener() {
            @Override
            public void onResponse(String requestId) {
                LoginEventArgs args = new LoginEventArgs(process.getStatus(), process.isSuccess());
                args.setId(process.gettId());
                fireEvent(args, listener);
            }
        });
    }

    /**
     * 注册
     *
     * @param name
     * @param pwd
     * @param listener
     */
    public void register(String name, String pwd, final EventListener listener) {
        final LoginRegisterProcess process = new LoginRegisterProcess(LoginRegisterIndex.REGISTER, name, pwd);
        process.runGet(new ResponseListener() {
            @Override
            public void onResponse(String requestId) {
                SimpleEventArgs<Boolean> args = new SimpleEventArgs<>(process.getStatus(), process.isSuccess());
                fireEvent(args, listener);
            }
        });
    }
}
